/*
 * dataset.h
 *
 *  Created on: Feb 28, 2011
 *      Author: tqlong
 */

#ifndef DATASET_H_
#define DATASET_H_

#include <vector>
#include <iostream>
#include <fstream>
#include <armadillo>

class Example {
  friend class Dataset;
  arma::vec x_;
  double y_;

  Example(const arma::vec& x, double y) : x_(x), y_(y) {}
  Example(int dim = 1) : x_(dim) {}
  const arma::vec& x() const { return x_; }
  double y() const { return y_; }

  void save(const std::string& fname);
  void save(std::ostream& f);
  bool load(const std::string& fname);
  bool load(std::istream& f);
};

class Dataset
{
  std::vector<Example> p_;
  int dim_;
  std::string fname_;
  std::ifstream f_;
  bool bias_;
public:
  Dataset(const std::string& fname, bool bias_ele = true); // online version
  Dataset() {}  // batch version
  ~Dataset() { f_.close(); }

  void save(const std::string& fname);
  void save(std::ostream& f);
  void load(const std::string& fname, bool bias_ele = true);
  void load(std::istream& f, bool bias_ele = true);
  bool next();
  void restart();
  void generate(int n, int d, double noise = 0.0);

  const int dim() const { return dim_; }
  const int n() const { return p_.size(); }
  const arma::vec& x(int i = 0) const { return p_[i].x(); }
  double y(int i = 0) const { return p_[i].y(); }
};

#endif /* DATASET_H_ */
